var MY_CLINIC_DPT_COMMON = (function() {
	function getDpts() {
		var dpts = null;
		$.ajax({ 
			type: "POST", 
			url: "wncrm_clinic_dpt.do/load", 
			dataType: "json",
			data: null,
			async: false,
			success: function(data) {
				if (data.errcode == 0) {
					dpts = data.dpts;
				} else {
					WeKnow.systemMsg("系统异常");
				}
			},
			error: function() {
				WeKnow.systemMsg("系统异常");
			}
		});
		
		return dpts;
	}
	
	/**
	 * 生成Select控件的body
	 */
	function createSelectBody(dptCd, isFirstBlank) {
		var options = '';
		if (typeof isFirstBlank != 'undefined') {
			options += '<option value="">未指定科室</option>';
		}
		var dpts = getDpts();
		if (dpts && dpts.length) {
			$(dpts).each(function(idx, dpt) {
				options += '<option value="' + dpt.dptCd + '"' + ((typeof dptCd != 'undefined' && dptCd == dpt.dptCd)?'selected="selected"':'') + '>';
					options += dpt.dptName;
				options += '</option>';
			});
		}
		return options;
	}
	
	function searchDoctors($doctors, dptCd, isFirstBlank) {
		var postUrl = "wncrm_outpatient_register.do/getDoctors";
		var params = {};
		if (typeof dptCd != 'undefined' && !$.isEmptyObject(dptCd)) {
			params.dptCd = dptCd;
		}
		$.post(postUrl, params, function(data, status) {
			if(data.errorcode == 0) {
				$doctors.html((function() {
					var e = '';
					if (typeof isFirstBlank != 'undefined') {
						e += '<option  value="">未指定医生</option>';
					}
					$(data.doctors).each(function(idx, p) {
						if(p.role == 1){
							e += '<option  value="' + p.doctorId + '" >' + p.name + '</option>';
						}
					});
					return e;
				})());
			} else {
				WeKnow.systemMsg('系统异常');
			}
		});
	}
	
	return {
		getDpts: getDpts
		,createSelectBody: createSelectBody
		,searchDoctors: searchDoctors
	}
})();